package com.ball.dev.service.impl;

import com.ball.common.dtos.ResponseResult;
import com.ball.common.enums.AppHttpCodeEnum;
import com.ball.common.utils.CollUtils;
import com.ball.common.utils.UserContext;
import com.ball.dev.entity.po.Care;
import com.ball.dev.entity.vo.CareVo;
import com.ball.dev.mapper.CareMapper;
import com.ball.dev.service.ICareService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 关注表 服务实现类
 * </p>
 *
 * @author lou
 * @since 2024-03-04
 */
@Service
public class CareServiceImpl extends ServiceImpl<CareMapper, Care> implements ICareService {

    @Override
   public ResponseResult addCare(Care careDto) {
        //判断传入的参数是否为空
        if (careDto == null && careDto.getStatus()==null) {
            //如果为空，返回错误信息
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "关注信息不能为空");
        }

        //判断传入的参数是否为空
        if (careDto.getUserId() == null || careDto.getCareId() == null) {
            //如果为空，返回错误信息
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "关注信息不能为空");
        }
        //判断关注还是取消关注 0关注  1 取消关注
        if (careDto.getStatus() == 0){

            //查询是否已经关注
            Care care = lambdaQuery().eq(Care::getCareId, careDto.getCareId())//查询条件
                    .eq(Care::getUserId, careDto.getUserId())//查询条件
                    .one();

            //第一次关注
            if (care == null){
                //设置创建时间和更新时间
                careDto.setCreateTime(LocalDateTime.now());
                careDto.setUpdateTime(LocalDateTime.now());

                //保存关注信息
                return save(careDto)? ResponseResult.okResult("关注成功")
                        : ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"添加失败");
            }else {
                //更新关注信息
                boolean result = lambdaUpdate().eq(Care::getUserId, careDto.getUserId())//查询条件
                        .eq(Care::getCareId, careDto.getCareId())//查询条件
                        .set(Care::getStatus, 0)//将状态改为0
                        .set(Care::getUpdateTime, LocalDateTime.now()).update();//将更新时间更新为现在
                return result ? ResponseResult.okResult("关注成功"): ResponseResult.okResult("网络延迟，请稍后再试");

            }


        }else {
            //取消关注
            boolean result = lambdaUpdate().eq(Care::getUserId, careDto.getUserId())
                    .eq(Care::getCareId, careDto.getCareId())
                    .set(Care::getStatus, 1)
                    .set(Care::getUpdateTime, LocalDateTime.now()).update();
            return result ? ResponseResult.okResult("取消关注成功"): ResponseResult.okResult("网络延迟，请稍后再试");
        }



    }

    @Override
    public ResponseResult getCareByUserId(Long id) {
        //判断id是否为空
        if (id==null){
            //如果为空，返回错误信息
            ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户id不能为空");
        }

        //根据id查询关注信息
       List<CareVo> vos =  baseMapper.getCares(id);

        //判断查询结果是否为空
        if (CollUtils.isEmpty(vos)){
            //如果为空，返回错误信息
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"没有关注信息");
        }

        //返回查询结果
        return ResponseResult.okResult(vos);

    }


}
